import { request } from "./request.js";

//查询全部角色列表
export function getRoleList() {
  return request({
    method: "get",
    url: "/role",
  });
}
//查询角色分页列表
export const getRolePageList = (queryInfo) => {
  return request({
    method: "get",
    url: "/role/pageInfo",
    params: queryInfo,
  });
};
//获得单个角色信息
export function getRoleInfo(id) {
  return request({
    method: "get",
    url: `/role/${id}`,
  });
}
// 更新单个角色的某个字段
export function patchRole(id, patchDocument) {
  return request({
    method: "patch",
    url: `/role/${id}`,
    data: patchDocument, //参数为类型为object,不用加花括号进行参数指定
  });
}
//添加角色
export function addRole(roleInfo) {
  return request({
    method: "post",
    url: "/role",
    data: roleInfo,
  });
}
//修改角色
export function putRoleInfo(roleInfo) {
  return request({
    method: "put",
    url: "/role",
    data: roleInfo,
  });
}
//删除角色
export function deleteRoleById(id) {
  return request({
    method: "delete",
    url: `/role/${id}`,
  });
}
//批量删除角色
export const deleteRoles = (ids) => {
  return request({
    method: "delete",
    url: "role",
    data: ids,
  });
};
//获得用户列表
export const getRoleUserList = (roleId) => {
  return request({
    method: "get",
    url: `/role/${roleId}/roleUsers`,
  });
};
//为角色绑定用户
export const allotRoleUser = (roleId, userIds) => {
  return request({
    method: "post",
    url: `/role/${roleId}/roleUsers`,
    data: userIds,
  });
};
//删除角色绑定的权限
export function removePermissionById(roleId, permissionId) {
  return request({
    method: "delete",
    url: `/role/${roleId}/permission/${permissionId}`,
    //data:{}
  });
}

//设置用角色权限
export function setRolePermissions(roleId, idStr) {
  // axios默认对请求体序列化,后端拿不到string类型的参数, 需要用对象接收
  //通过URLSearchParams可以使请求Content-type变成"application/x-www-form-urlencoded",后端可以在Request.Form
  const params = new URLSearchParams();
  params.append("ids", idStr);
  return request({
    method: "post",
    url: `/role/${roleId}/permission`,
    data: params,
  });
}

//获取角色绑定的权限id
export const getPermissionsByRoleId = (roleId) => {
  return request({
    method: "get",
    url: `/role/${roleId}/permission`,
  });
};
